package com.superiron.scaffold.core.service.executor;

import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.AsyncResult;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.concurrent.Future;

/**
 * @author ：yzg
 * @date ：Created in 2022/6/10 9:47
 * @description：异步线程实现
 * @modified By：
 * @version: $
 */
@Service
@Slf4j
public class AsynThreadServiceTest {
	
	public static class AsyncDeviceEvent extends MyRunnable {
		List<Integer> list;
		
		public AsyncDeviceEvent(List<Integer> list) {
			this.list = list;
		}
		
		@Override
		public void doRun() {
			log.info("处理开始-------------->");
			try {
				for (Integer integer : list) {
					Thread.sleep(100);
				}
				log.info("处理完成：{}", list.size());
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
			new AsyncResult(list);
		}
	}
	
	@Async("asyncServiceExecutor")
	public void executeAsync(List<Integer> list) {
		log.info("处理开始-------------->");
		try {
			for (Integer integer : list) {
				Thread.sleep(100);
			}
			log.info("处理完成：{}", list.size());
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
		
	}
	
	@Async("asyncServiceExecutor")
	public Future<List<Integer>> executeAsyncReturn(List<Integer> list) {
		log.info("线程开始执行---------->");
		try {
			for (Integer integer : list) {
				Thread.sleep(100);
			}
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
		
		return new AsyncResult(list);
	}
	
	public List<Integer> executeAsyncReturnNo(List<Integer> list) {
		
		try {
			for (Integer integer : list) {
				Thread.sleep(100);
			}
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
		
		return list;
	}
}
